package com.ezjie.ielts.module_word;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.application.YsApplication;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.core.http.HttpRequestAbstractCallBack;
import com.ezjie.ielts.model.FilterWordData;
import com.ezjie.ielts.model.FilterWordResponse;
import com.ezjie.ielts.model.WordBean;
import com.ezjie.ielts.module_word.adapter.FirstStudySummaryAdapter;
import com.ezjie.ielts.task.WordTask;
import com.ezjie.ielts.util.AppUtil;
import com.ezjie.ielts.util.LogUtils;
import com.ezjie.ielts.util.NetworkUtil;
import com.ezjie.ielts.util.PreferencesUtil;
import com.ezjie.ielts.util.UmengPages;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.layout_firststudy_summary)
public class WordFirstStudySummaryActivity extends BaseFragmentActivity {
	private static final String TAG = WordFirstStudySummaryActivity.class.getSimpleName();
	private Context mContext;
	@ViewInject(R.id.navi_back_btn)
	private ImageView backBtn;// 返回按钮
	@ViewInject(R.id.next_learn)
	private Button next_study;// 下一组复习
	@ViewInject(R.id.next_review)
	private Button next_review;// 复习
	@ViewInject(R.id.navi_title_text)
	private TextView review_title;// 新学词汇
	@ViewInject(R.id.choose_review)
	private TextView choose_review;// 完成第一次学习
	@ViewInject(R.id.review_list)
	private ExpandableListView review_list;
	@ViewInject(R.id.firststuday_summary_rl)
	private RelativeLayout firststuday_summary_rl;

	private List<WordBean> mWordBean;
	private FirstStudySummaryAdapter mAdapter;
	private int lastItem = -1;

	private WordTask mWordTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		ViewUtils.inject(this);
		mContext = this;
		mWordBean = new ArrayList<WordBean>();
		mWordTask = new WordTask(this);

		init();
	}

	private void init() {
		backBtn.setOnClickListener(this);
		next_study.setOnClickListener(this);
		next_review.setOnClickListener(this);
		review_title.setText(R.string.word_first_study_title);
		choose_review.setText(R.string.word_first_study_summary_gongxi1);
		next_study.setText(R.string.word_practice_new_word);
		int firststuday_summary_bg = PreferencesUtil.getInt(mContext, "word_study_bg", 0);
		int[] images = YsApplication.getImgs();
		firststuday_summary_rl.setBackgroundResource(images[firststuday_summary_bg]);

		YsApplication myApplication = (YsApplication) getApplication();
		mWordBean = myApplication.getWords();

		mAdapter = new FirstStudySummaryAdapter(mContext);
		mAdapter.setmList(mWordBean);
		review_list.setAdapter(mAdapter);

		review_list.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				if (lastItem >= 0 && lastItem != groupPosition) {
					review_list.collapseGroup(lastItem);
				}
				lastItem = groupPosition;
			}
		});

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_WORD_FIRST_DTUDY_SUMMARY);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_WORD_FIRST_DTUDY_SUMMARY);
	}

	/**
	 * 获取筛词数据
	 */
	private void getShaiciWord() {
		if (!NetworkUtil.isNetworkAvailable(this)) {
			AppUtil.showToast(this, R.string.no_network);
			return;
		}

		mWordTask.getShaiciWord("simple", new HttpRequestAbstractCallBack(mContext,true) {
			@Override
			public void onStartCallBack() {
				super.onStartCallBack();
				AppUtil.showProgressDialog(mContext);
			}

			@Override
			public void onSuccessCallBack(String result) {
				super.onSuccessCallBack(result);
				LogUtils.d(result);
				AppUtil.dismissProgressDialog();
				try {
					FilterWordResponse filterWordResponse = JSON.parseObject(result, FilterWordResponse.class);
					if (filterWordResponse != null && 200 == filterWordResponse.getStatus_code()) {
						String data = filterWordResponse.getData();
						if (!TextUtils.isEmpty(data)) {
							FilterWordData filterWordData = JSON.parseObject(data, FilterWordData.class);

							YsApplication myApplication = (YsApplication) getApplication();
							myApplication.setFilterWordData(filterWordData);

							startActivity(new Intent(mContext, WordFilterActivity.class));
						}
					}
				} catch (Exception e) {
					LogUtils.d("json数据异常");
					LogUtils.exception(e);
				}
			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				super.onFailureCallBack(error, msg);
				LogUtils.e(msg);
				AppUtil.dismissProgressDialog();
				AppUtil.showToast(mContext, R.string.load_net_data_failure);
			}
		});
	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.navi_back_btn:
			finish();
			break;
		case R.id.next_learn:
			//友盟统计
			MobclickAgent.onEvent(mContext, "word_learn_next_wordBtn");
			
			// 下一组学习
			getShaiciWord();
			break;
		case R.id.next_review:
			// 复习
			Intent errorIntent = new Intent(this, ReviewDetailActivity.class);
			errorIntent.putExtra("type", 0);
			startActivity(errorIntent);
			finish();
			break;
		}
	}
}
